import React, { PureComponent } from 'react';
import { connect } from 'dva';
import { Spin } from 'antd';
import { SecondHeaderC, NoDataC } from '../../../../comps';
import AssetTable from './AssetTable';

@connect(({ nonstandardReportM, loading }) => ({
  nonstandardReportM,
  loading: loading.effects['nonstandardReportM/getTableData'],
}))
export default class tabPage extends PureComponent {
  state = {
    dataSource: {},
    dataKeys: [],
    productData: {
      data: {
        productName: 'productName',
      },
      dataSource: [
        {
          liabilitySideDate: '2022-04-15',
          liabilitySideEvent: '计息',
          cashInFlow: 9999999.65,
          cashOutFlow: 999854.25,
          cashFlow: -100,
          spread: -0.05,
          id: 'adfasdfsd',
        },
      ],
    },
    groupData: [
      {
        data: {
          groupName: 'groupName',
        },
        dataSource: [],
      },
    ],
  };
  componentDidMount() {
    this.props.onRef && this.props.onRef(this);
  }
  // 查询tab下对应信息
  getData = () => {
    const that = this;
    const { tabData, dispatch } = this.props;
    let params = {};
    if (tabData.productId) {
      params.productId = tabData.productId;
    }
    if (tabData.groupList?.length) {
      params.groupList = tabData.groupList.join(',');
    }
    dispatch({
      type: 'nonstandardReportM/getTableData',
      payload: {
        queryKey: 'MaatLiabilitySideReport',
        queryConditions: {
          ...params,
        },
      },
      callback: (msg, res) => {
        if (msg == 'success') {
          let _data = res.dataSource || {};
          let _dataKey = [];
          for (let key in res.dataSource) {
            _dataKey.push(key);
          }
          //通知父组件 有无产品利差
          if (this.props.getReportData) {
            let temp = Object.values(_data)[0];
            this.props.getReportData(temp[0]?.spread);
          }
          that.setState({
            dataKeys: _dataKey,
            dataSource: _data,
          });
        }
      },
    });
  };
  render() {
    const { dataKeys, dataSource } = this.state;
    const { loading } = this.props;
    return (
      <Spin spinning={loading}>
        {dataKeys?.length ? (
          dataKeys.map((item) => {
            return (
              <div key={item}>
                <SecondHeaderC title={item} />
                <AssetTable
                  dataSource={(dataSource[item] || []).map((rowData) => {
                    rowData.children = null;
                    return rowData;
                  })}
                />
              </div>
            );
          })
        ) : (
          <NoDataC />
        )}
      </Spin>
    );
  }
}
